package com.zf.yichat;

import com.github.pagehelper.PageHelper;
import com.zf.yichat.config.EnvConfig;
import org.apache.ibatis.plugin.Interceptor;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.io.ClassPathResource;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver;

import java.util.Properties;
import java.util.concurrent.Executor;

@EnableAspectJAutoProxy
@tk.mybatis.spring.annotation.MapperScan(basePackages = {"com.zf.yichat.mapper"})
@SpringBootApplication(scanBasePackages = {"com.zf.yichat"})
@EnableScheduling
@EnableAsync
public class WebApplication {

    public static void main(String[] args) {
        ConfigurableApplicationContext run = SpringApplication.run(WebApplication.class, args);

        // 在类中无法被spring管理时获取配置文件
        ConfigurableEnvironment environment = run.getEnvironment();
        // 获取socket访问路径
        String socketPath = environment.getProperty("socket.path");
//        String rsaPrivatePath = environment.getProperty("rsa_key.private_path");
        EnvConfig.setSocketPath(socketPath);
//        EnvConfig.setRsaKeyPath(rsaPrivatePath);
    }



    /**
     * 加载yml配置文件,根目录为resources
     *
     * @return
     */
    @Bean
    public static PropertySourcesPlaceholderConfigurer properties() {
        PropertySourcesPlaceholderConfigurer pspc = new PropertySourcesPlaceholderConfigurer();
        YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean();
        yaml.setResources(new ClassPathResource[]{

        });
        pspc.setProperties(yaml.getObject());
        return pspc;
    }




    @Bean(name = "threadPoolTaskExecutor")
    public Executor threadPoolTaskExecutor() {
        return new ThreadPoolTaskExecutor();
    }
}
